//sendValidateCode
import { NextResponse } from "next/server"
import {Email} from '@/lib/mail'
import {checkCodeGenerateFrequently,generateNewCode} from '@/server/database/repositories/validationCodesRepository'
export async function POST( req: Request ) {
  try {
      const body = await req.json()
      const {email} = body
      if (!email) {
        return NextResponse.json('请输入邮箱', { status: 400 })
      }

      const result = await checkCodeGenerateFrequently(email)
      if(!result){
        return NextResponse.json('请求太频繁了', { status: 429 })
      }

      const emailBot = new Email()
      const code = await emailBot.send({email})
      await generateNewCode(email, code as string)
      return NextResponse.json( '请在邮箱查看验证码 !' )
  } catch (error) {
    return NextResponse.json(error,{status: 500})
  }
}

